package com.naiterui.ehp.bp.support.utils.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;

/**
 * 字符串时间转换为日期类型
 */
public class DateBindingInitializer implements WebBindingInitializer {

    @Override
    public void initBinder(@NonNull WebDataBinder webDataBinder) {
        // 可以設定任意的日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat1, true));
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat2, true));
    }

}
